import pygame
from vec2d import Vec2d
from body import Body
from pygame.sprite import Sprite
from pygame.color import Color
from sympy.geometry import Ellipse,Point
import constants

class SystemPoint:
    
    def __init__(self, x_or_Vec2d, y = None):
        """ The coordinates are system coordinates.
        """
        if isinstance(x_or_Vec2d, Vec2d):
            self.x = x_or_Vec2d.x
            self.y = x_or_Vec2d.y
        else:
            self.x = x_or_Vec2d
            self.y = y
            
    @property
    def vect(self):
        return Vec2d(self.x, self.y)        
    
    @property
    def system_coords(self):
        return (self.x, self.y)
    
    @property
    def screen_coords(self):
        return (int(self.x), int(-self.y))
    
    def __repr__(self):
        return self.system_coords()
    
    
class Orbit():

    def __init__(self, center = Point(0,0), hr = 2, vr = 1):
        self.path = Ellipse(center,hr,vr)

class Body:
    
    def __init__(self, pos = Vec2d(0,0), color = "blue", radius = 1, mass = 10000.0):
        self.radius = radius
        if isinstance(pos, Vec2d):
            self.pos = pos
        else:
            self.pos = Vec2d(pos)
        self.accel = 0
        self.velocity = 0
        self.mass = mass
        self.color = pygame.color.Color(color)
        
    def update(self):
        pass
    
    def draw(self):
        pass
        
class Planet(Body):
         
    def __init__(self, pos = (0,0), color = "blue", radius = 1, mass = 10000):
        Body.__init__(self, pos, color, radius, mass)
    
    def update(self, system, game):
        if self == system.sun:
            return
        
        #for x in range(1,10):
        accel = constants.G*system.sun.mass*((self.pos - system.sun.pos)/abs(self.pos - system.sun.pos)**3)
        self.velocity += accel * game.dt
        self.pos += self.velocity * game.dt
            
    def old_update(self, system, game):
        accel = Vec2d(0,0)
        try:
            accel = -(self.pos / (abs(self.pos) ** 3))
        except ZeroDivisionError:
            accel = Vec2d(0,0)
        self.velocity += accel * game.dt
        self.pos += self.velocity * game.dt
        new_coords = self.pos + system.sun.pos
        
        
    def draw(self, surface, scale = 20):
        pygame.draw.circle(surface, self.color,self.pos.get_screen_coords(), self.radius)
            #pygame.draw.arc(surface, pygame.color.Color("blue"), False, self.old_positions)
        
    def planet_added(self, system):
        self.system = system
        
    
class System():
    
    def __init__(self, pos = Vec2d(0,0)):
        self.sun = Planet(radius = 4)
        self.display = pygame.display.get_surface()
        self.planets = []
        self.ships = []
        self.pos = pos
        
    def set_sun(self, sun):
        self.sun = sun
        
    def add_planet(self, planet):
        self.planets.append(planet)
        planet.planet_added(self)
        
    def update(self, game):
        self.sun.update(self, game)
        for planet in self.planets:
            planet.update(self, game)

    def draw(self, surface):
        self.sun.draw(surface)
        for planet in self.planets:
            planet.draw(surface)
            
        #self.player.draw()
    
        